public class Test2 {
    //main1
    //private static Object locker1=new Object();
    //private static Object locker2=new Object();

    private static Object locker1 = new Object();
    private static Object locker2 = new Object();
    private static Object locker3 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker1) {
                        locker1.wait();
                    }
                    System.out.print("A");
                    synchronized (locker2) {
                        locker2.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker2) {
                        locker2.wait();
                    }
                    System.out.print("B");
                    synchronized (locker3) {
                        locker3.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker3) {
                        locker3.wait();
                    }
                    System.out.println("C");
                    synchronized (locker1) {
                        locker1.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);

        // 从线程 t1 启动
        synchronized (locker1) {
            locker1.notify();
        }
    }


    public static void main1(String[] args) {
        Thread t1=new Thread(()->{
            System.out.print("A");

            //未防止t1提前执行了notify，然后t2再执行了wait，会使t2的wait无法唤醒,只需进行一个sleep即可
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized(locker1){
                locker1.notify();
            }
        });

        Thread t2=new Thread(()->{
            synchronized(locker1){
                try{
                    locker1.wait();
                }catch (InterruptedException e){
                    throw new RuntimeException(e);
                }
            }
            System.out.print("B");

            synchronized (locker2){
                locker2.notify();
            }
        });

        Thread t3=new Thread(()->{
            synchronized(locker2){
                try {
                    locker2.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("C");
        });

        t1.start();
        t2.start();
        t3.start();

    }
}
